//////////////////////////////////////
// Fast Container Library           //
// MIT License - Copyright (c) 2012 //
//////////////////////////////////////

#pragma once

#include "deque.h"
#include "fixed_allocator.h"


FC_NAMESPACE_BEGIN


template <class T, size_t N, class Allocator>
class fixed_deque : public deque<T, Allocator>
{
public:
	typedef fixed_deque<T, N, Allocator>				this_type;
	typedef deque<T, Allocator>							base_type;

	using base_type::m_data;
	using base_type::m_head;
	using base_type::m_tail;
	using base_type::m_capacity;
	using base_type::m_size;
	using base_type::m_allocator;

	enum
	{
		buffer_size_n_bytes = N * sizeof(T),
		buffer_alignment = FC_ALIGNOF(T)
	};

	typedef aligned_buffer<buffer_size_n_bytes, buffer_alignment> buffer_type;

	fixed_deque()
		: base_type()
		{
			init();
		}

	explicit fixed_deque( size_t n, const T& value = T() )
		: base_type()
		{
			init();
			base_type::assign(n, value);
		}

	template<class InputIterator>
		fixed_deque( InputIterator first, InputIterator last )
		: base_type()
		{
			init();
			base_type::assign( first, last );
		}

	fixed_deque( const this_type& x )
		: base_type()
		{
			init();
			base_type::assign( x.begin(), x.end() );
		}

	this_type& operator =( const this_type& x )
	{
		base_type::operator =(x);
		return *this;
	}

	bool has_overflowed() const
	{
		return m_data != get_buffer();
	}

	void reset()
	{
		if( m_capacity )
		{
			base_type::clear();
			base_type::deallocate();
		}

		init();
	}

	void init()
	{
		m_data = m_head = m_tail = (T*)m_buffer.data;
		m_capacity = m_data + N;
		m_size = 0;
		m_allocator.set_buffer(m_data);
	}

	T* get_buffer() const
	{
		return (T*)m_buffer.data;
	}

protected:
	buffer_type m_buffer;

};



FC_NAMESPACE_END
